package com.project.harmonyledger.controller;

import com.project.harmonyledger.common.response.Result;
import com.project.harmonyledger.dto.StatisticsParams;
import com.project.harmonyledger.dto.TransactionParams;
import com.project.harmonyledger.pojo.Transaction;
import com.project.harmonyledger.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/transactions")
public class TransactionController {
  @Autowired
  private TransactionService transactionService;

  @PostMapping("/create")
  public Result<Map<String, String>> createTransaction(@RequestBody TransactionParams params) {
    return transactionService.createTransaction(
        params.getAccountId(),
        params.getCategoryId(),
        params.getAmount(),
        params.getType(),
        params.getDescription(),
        params.getTransactionDate());
  }

  @DeleteMapping("/deleteTransaction")
  public Result<Map<String, String>> deleteTransaction(@RequestParam Long transactionId) {
    return transactionService.deleteTransaction(transactionId);
  }

  @PutMapping("/updateTransaction")
  public Result<Map<String, String>> updateTransaction(
      @RequestParam Long transactionId,
      @RequestBody TransactionParams params) {
    return transactionService.updateTransaction(
        transactionId,
        params.getCategoryId(),
        params.getAmount(),
        params.getDescription());
  }

  @GetMapping("/getTransactionById")
  public Result<Map<String, String>> getTransactionById(@RequestParam Long transactionId) {
    return transactionService.getTransactionById(transactionId);
  }

  @GetMapping("/getTransactionsByAccountId")
  public Result<List<Transaction>> getTransactionsByAccountId(@RequestParam Long accountId) {
    return transactionService.getTransactionsByAccountId(accountId);
  }

  @GetMapping("/getAccountStatistics")
  public Result<Map<String, Object>> getAccountStatistics(@RequestBody StatisticsParams params) {
    return transactionService.getAccountStatistics(
        params.getAccountId(),
        params.getStartDate(),
        params.getEndDate());
  }

  @GetMapping("/getAllAccountsStatistics")
  public Result<Map<String, Object>> getAllAccountsStatistics(
      @RequestParam String userId,
      @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
      @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
    return transactionService.getAllAccountsStatistics(userId, startDate, endDate);
  }

  @GetMapping("/getUserTransactions")
  public Result<List<Transaction>> getUserTransactions(
      @RequestParam String userId,
      @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
      @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
    return transactionService.getUserAllTransactions(userId, startDate, endDate);
  }
}